package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.information.domain.back.CloudClassroomProjectMainBack;
import com.ruoyi.information.mapper.back.CloudClassroomBackMapper;
import com.ruoyi.information.mapper.back.CloudClassroomProjectMainBackMapper;
import com.ruoyi.information.service.back.ICloudClassroomProjectMainBackService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.PathVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 云课堂专辑Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-28
 */
@Service
public class CloudClassroomProjectMainBackServiceImpl implements ICloudClassroomProjectMainBackService {
    @Autowired
    private CloudClassroomProjectMainBackMapper cloudClassroomProjectMainMapper;


    @Autowired
    private CloudClassroomBackMapper classroomMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询云课堂专辑
     *
     * @param id 云课堂专辑主键
     * @return 云课堂专辑
     */
    @Override
    public CloudClassroomProjectMainBack selectCloudClassroomProjectMainById(Long id) {
        return cloudClassroomProjectMainMapper.selectCloudClassroomProjectMainById(id);
    }

    /**
     * 查询云课堂专辑列表
     *
     * @param cloudClassroomProjectMain 云课堂专辑
     * @return 云课堂专辑
     */
    @Override
    public List<CloudClassroomProjectMainBack> selectCloudClassroomProjectMainList(CloudClassroomProjectMainBack cloudClassroomProjectMain) {
        return cloudClassroomProjectMainMapper.selectCloudClassroomProjectMainList(cloudClassroomProjectMain);
    }

    /**
     * 新增云课堂专辑
     *
     * @param cloudClassroomProjectMain 云课堂专辑
     * @return 结果
     */
    @Override
    public int insertCloudClassroomProjectMain(CloudClassroomProjectMainBack cloudClassroomProjectMain) {
        cloudClassroomProjectMain.setCreateTime(DateUtils.getNowDate());
        return cloudClassroomProjectMainMapper.insertCloudClassroomProjectMain(cloudClassroomProjectMain);
    }

    /**
     * 修改云课堂专辑
     *
     * @param cloudClassroomProjectMain 云课堂专辑
     * @return 结果
     */
    @Override
    public int updateCloudClassroomProjectMain(CloudClassroomProjectMainBack cloudClassroomProjectMain) {
        cloudClassroomProjectMain.setUpdateTime(DateUtils.getNowDate());

        /*删除云资源*/
        if (cloudClassroomProjectMain.getCover() != null) {
            CloudClassroomProjectMainBack main = cloudClassroomProjectMainMapper.selectCloudClassroomProjectMainById(cloudClassroomProjectMain.getId());
            if (main.getCover()!=null){
                CloudClassroomBackServiceImpl.comparePhoto(main.getCover(), cloudClassroomProjectMain.getCover(), remoteFileService);
            }
        }
        return cloudClassroomProjectMainMapper.updateCloudClassroomProjectMain(cloudClassroomProjectMain);
    }

    /**
     * 批量删除云课堂专辑
     *
     * @param ids 需要删除的云课堂专辑主键
     * @return 结果
     */
    @Override
    public int deleteCloudClassroomProjectMainByIds(Long[] ids) {
        /*级联删除云课堂*/
        List<String> photos = classroomMapper.selectCloudClassroomByProjectIdsPhotos(ids);
        if (photos != null) {
            remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);
        }
        classroomMapper.deleteCloudClassroomByProjectIds(ids);
        /*删除云资源*/
        photos = cloudClassroomProjectMainMapper.selectCloudClassroomProjectMainListPhoto(ids);
        if (photos != null) {
            remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);
        }
        return cloudClassroomProjectMainMapper.deleteCloudClassroomProjectMainByIds(ids);
    }

    /**
     * 删除云课堂专辑信息
     *
     * @param id 云课堂专辑主键
     * @return 结果
     */
    @Override
    public int deleteCloudClassroomProjectMainById(Long id) {
        return cloudClassroomProjectMainMapper.deleteCloudClassroomProjectMainById(id);
    }
}
